/*
 * @Author: lsjweiyi 759209794@qq.com
 * @Date: 2024-04-04 23:05:13
 * @LastEditors: lsjweiyi 759209794@qq.com
 * @LastEditTime: 2024-05-12 10:48:56
 * @FilePath: \web-ssg\pages\image\imgApi.ts
 * @Description: 图片功能接口
 */
export interface IChangebgColor {
    file: File;
    color: BGRA | undefined;
}
export interface IImgCompress {
    file: File;
    expectSize: number;
}

export interface BGRA {
    b: number;
    g: number;
    r: number;
    a: number;
}
export interface IDocType {
    message?: string;
    word: boolean;
    excel: boolean;
}

export interface IDocTypeDownUrl {
    word: string;
    excel: string;
}

export function cardChangeBackground(img: IChangebgColor, toolId: number) {
    let formdata = new FormData();
    formdata.append("file", img.file);
    if (img.color) {
        formdata.append("color", JSON.stringify(img.color));
    }
    return post<string>("cardChangeBGColor", requestConfig(formdata, toolId));
}

// 图片有损压缩
export function imgLossyCompress(file: File, needSize: number, toolId: number) {
    let formdata = new FormData();
    formdata.append("file", file);
    formdata.append("needSize", needSize.toString());
    return post<string>("imgLossyCompress", requestConfig(formdata, toolId));
}

// png无损压缩
export function pngLosslessCompress(file: File, level: number, toolId: number) {
    let formdata = new FormData();
    formdata.append("file", file);
    formdata.append("level", level.toString());
    return post<number | string>("pngLosslessCompress", requestConfig(formdata, toolId));
}
// 图片格式转换
export function imgConvers(file: File, suffix: string, toolId: number) {
    let formdata = new FormData();
    formdata.append("file", file);
    formdata.append("suffix", suffix);
    return post<string>("imgConvers", requestConfig(formdata, toolId));
}
// 图片转文档，这是异步完成的
export function img2doc(file: File, taskId: string, toolId: number) {
    let formdata = new FormData();
    formdata.append("file", file);
    return post<string>("img2doc", requestConfig(formdata, toolId, taskId));
}
// 需要用该接口去查询结果
export function img2docResult(taskId: string, toolId: number) {
    return get<IDocType | string>("img2docResult", requestConfig(undefined, toolId, taskId));
}
// 获取支持的类型
// export function img2docDownType(taskId: string, toolId: number) {
//     return get<IDocType | string>("img2docDownType", {
//         headers: {
//             "task-id": taskId,
//             "tool-id": toolId
//         },
//     });
// }
// 获取对应的类型的下载链接
export function img2docDownUrl(taskId: string, docType: string, toolId: number) {
    return get<string>("img2docDown/" + docType, requestConfig(undefined, toolId, taskId));
}

/**
 * 图像特效处理，toold取：21-26
 *
 * @param file 待处理的文件
 * @param toolId 工具ID
 * @param taskId 任务ID
 * @returns Promise<Blob> 处理后的Blob对象
 */
export function imgEffectEnhance(file: File, toolId: number) {
    let formdata = new FormData();
    formdata.append("file", file);
    formdata.append("toolId", toolId.toString());
    return post<string>("imgEffectEnhance", requestConfig(formdata, toolId));
}

export function imgDown(taskId: string, toolId: number) {
    return get<string>(
        "/imgDown/" + toolId + "/" + taskId,
        undefined,
        true
    );
}

function requestConfig(formdata?: FormData, toolId?: number, taskId?: string) {
    let requestConfig = {} as any
    if (formdata) {
        requestConfig.body = formdata
    }
    if (toolId) {
        requestConfig.headers = {
            "tool-id": toolId
        }
    }
    if (taskId) {
        requestConfig.headers["task-id"] = taskId
    }
    return requestConfig
}
